package com.food.mail;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.food.model.Course;
import com.food.model.Member;

public class Mail_Test {
	static final String username = "eeitfoodgod01@gmail.com";
	static final String password = "sa123456sa";
	
	static public void send(String from_mail,Member member,Course course)
	{
		Properties props = new Properties();
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.port", "587");
 
		Session session = Session.getInstance(props,
		  new javax.mail.Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(username, password);
			}
		  });
 
		try {
 
			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress(from_mail));
			message.setRecipients(Message.RecipientType.TO,
				InternetAddress.parse(member.getMbemail()));
			message.setSubject("Food正神食譜分享社群--課程通知");
			message.setText("親愛"+member.getMbname()+"您好,"
				+ "\n 您參加的課程:"+course.getCname()
				+ "\n 課程講師:"+course.getCteacher()
				+ "\n 開始日期:"+course.getCstartdate()
				+ "\n 結束日期:"+course.getCenddate()
				+ "\n 上課地點:"+course.getCplace()
				+ "\n 請於繳費成功後準時參與,謝謝您的報名!"	);
 
			Transport.send(message);
 
			System.out.println("Done");
 
		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}

	}
	
	public static void main(String[] args) {
		 
	//	Mail_Test.send("yj", "96208059@nccu.edu.tw");
 
			}
}
